//
// DO NOT MODIFY THIS FILE - it is generated using templatec on BaseOutput.t
//
package lpg;

import java.io.PrintWriter;
import java.util.Arrays; /* BaseOutput.t:1 */
import java.util.BitSet; /* BaseOutput.t:2 */
import java.util.List; /* BaseOutput.t:3 */
import lpg.util.DispatchWriter; /* BaseOutput.t:4 */

class BaseOutput
{
    private PrintWriter out = null;

    public void setOut(PrintWriter pw)
    {
        out = pw;
    }

    private final DispatchWriter writer; /* BaseOutput.t:7 */

    private final Control control; /* BaseOutput.t:9 */
    private final Option option; /* BaseOutput.t:10 */
    private final Grammar grammar; /* BaseOutput.t:11 */
    private final Base base; /* BaseOutput.t:12 */

    BaseOutput(Control control, Base base) /* BaseOutput.t:14 */
    { /* BaseOutput.t:15 */
        this.control    = control; /* BaseOutput.t:16 */
        this.base       = base; /* BaseOutput.t:17 */

        option          = control.option; /* BaseOutput.t:19 */
        grammar         = control.grammar; /* BaseOutput.t:20 */

        setOut(option._syslis); /* BaseOutput.t:22 */
        writer = option._syslis; /* BaseOutput.t:23 */
    } /* BaseOutput.t:24 */

    // /* BaseOutput.t:26 */
    // PRINT_XREF prints out the Cross-reference map. We build a map from each /* BaseOutput.t:27 */
    // terminal into the set of items whose Dot-symbol (symbol immediately /* BaseOutput.t:28 */
    // following the dot ) is the terminal in question.  Note that we iterate /* BaseOutput.t:29 */
    // backwards over the rules to keep the rules associated with the items /* BaseOutput.t:30 */
    // sorted, since they are inserted in STACK fashion in the lists:  Last-in, /* BaseOutput.t:31 */
    // First out. /* BaseOutput.t:32 */
    // /* BaseOutput.t:33 */
    void printXref(VariableSymbol[] sortSym, int[] numSymbols, int[] nextItem) /* BaseOutput.t:34 */
    { /* BaseOutput.t:35 */
        // /* BaseOutput.t:36 */
        // First, flush any data left in the report buffer. /* BaseOutput.t:37 */
        // /* BaseOutput.t:38 */
        option.flushReport(); /* BaseOutput.t:39 */

        control.printHeading(); /* BaseOutput.t:41 */
        out.write("\n\nCross-reference table:\n"); /* BaseOutput.t:42~45 */
        for (int i = 0; i < grammar.numSymbols; i++) /* BaseOutput.t:46 */
        { /* BaseOutput.t:47 */
            int symbol = sortSym[i].symbolIndex(); /* BaseOutput.t:48 */
            if (symbol != grammar.acceptImage && symbol != grammar.eofImage && symbol != grammar.empty) /* BaseOutput.t:49 */
            { /* BaseOutput.t:50 */
                out.write("\n\n"); /* BaseOutput.t:51~53 */
                writer.beginAutoWrap(); /* BaseOutput.t:54 */

                out.print(grammar.restoreSymbol(grammar.retrieveString(symbol))); /* BaseOutput.t:56 */
                out.write(" ==>>"); /* BaseOutput.t:56 */

                writer.setIndentSize(writer.getCurrentColumn() + 1); /* BaseOutput.t:58 */

                if (grammar.isNonTerminal(symbol)) /* BaseOutput.t:60 */
                { /* BaseOutput.t:61 */
                    IntArrayList rules = grammar.rulesOfLhs(symbol); /* BaseOutput.t:62 */
                    for (int k = 0, m = rules.size(); k < m; k++) /* BaseOutput.t:63 */
                    { /* BaseOutput.t:64 */
                        out.print(rules.get(k)); /* BaseOutput.t:65 */
                    } /* BaseOutput.t:66 */
                } /* BaseOutput.t:67 */
                else /* BaseOutput.t:68 */
                { /* BaseOutput.t:69 */
                    for (int itemNo = numSymbols[symbol]; itemNo != Util.NIL; itemNo = nextItem[itemNo]) /* BaseOutput.t:70 */
                    { /* BaseOutput.t:71 */
                        out.print(base.itemTable[itemNo].ruleNumber); /* BaseOutput.t:72 */
                    } /* BaseOutput.t:73 */
                } /* BaseOutput.t:74 */

                writer.endAutoWrap(); /* BaseOutput.t:76 */
            } /* BaseOutput.t:77 */
        } /* BaseOutput.t:78 */
        out.write("\n\n"); /* BaseOutput.t:79~81 */
    } /* BaseOutput.t:82 */

    // /* BaseOutput.t:84 */
    // PRINT_NON_TERMINAL_FIRST prints the first set for each non-terminal. /* BaseOutput.t:85 */
    // /* BaseOutput.t:86 */
    void printNonTerminalFirst(BoundedArray<BitSet> ntFirst) /* BaseOutput.t:87 */
    { /* BaseOutput.t:88 */
        // /* BaseOutput.t:89 */
        // First, flush any data left in the report buffer. /* BaseOutput.t:90 */
        // /* BaseOutput.t:91 */
        option.flushReport(); /* BaseOutput.t:92 */

        control.printHeading(); /* BaseOutput.t:94 */
        out.write("\nFirst map for non-terminals:\n"); /* BaseOutput.t:95~97 */
        for (int nt = grammar.firstNonTerminal(); nt <= grammar.lastNonTerminal(); nt++) /* BaseOutput.t:98 */
        { /* BaseOutput.t:99 */
            out.write("\n\n"); /* BaseOutput.t:100~102 */
            out.print(grammar.restoreSymbol(grammar.retrieveString(nt))); /* BaseOutput.t:102 */

            writer.beginAutoWrap(); /* BaseOutput.t:104 */
            writer.setIndentSize(4); /* BaseOutput.t:105 */

            out.write(" ==>>"); /* BaseOutput.t:107 */
            for (int t = grammar.firstTerminal(); t <= grammar.lastTerminal(); t++) /* BaseOutput.t:108 */
            { /* BaseOutput.t:109 */
                if (ntFirst.get(nt).get(t)) /* BaseOutput.t:110 */
                { /* BaseOutput.t:111 */
                    out.print(grammar.restoreSymbol(grammar.retrieveString(t))); /* BaseOutput.t:112 */
                } /* BaseOutput.t:113 */
            } /* BaseOutput.t:114 */

            writer.endAutoWrap(); /* BaseOutput.t:116 */
        } /* BaseOutput.t:117 */
        out.write("\n"); /* BaseOutput.t:118~119 */
    } /* BaseOutput.t:120 */

    // /* BaseOutput.t:122 */
    // PRINT_FOLLOW_MAP prints the follow map. /* BaseOutput.t:123 */
    // /* BaseOutput.t:124 */
    void printFollowMap() /* BaseOutput.t:125 */
    { /* BaseOutput.t:126 */
        // /* BaseOutput.t:127 */
        // First, flush any data left in the report buffer. /* BaseOutput.t:128 */
        // /* BaseOutput.t:129 */
        option.flushReport(); /* BaseOutput.t:130 */

        control.printHeading(); /* BaseOutput.t:132 */
        out.write("\nFollow Map:\n"); /* BaseOutput.t:133~135 */
        for (int nt = grammar.firstNonTerminal(); nt <= grammar.lastNonTerminal(); nt++) /* BaseOutput.t:136 */
        { /* BaseOutput.t:137 */
            out.write("\n\n"); /* BaseOutput.t:138~140 */
            out.print(grammar.restoreSymbol(grammar.retrieveString(nt))); /* BaseOutput.t:140 */

            writer.beginAutoWrap(); /* BaseOutput.t:142 */
            writer.setIndentSize(4); /* BaseOutput.t:143 */

            out.write(" ==>>"); /* BaseOutput.t:145 */
            for (int t = grammar.firstTerminal(); t <= grammar.lastTerminal(); t++) /* BaseOutput.t:146 */
            { /* BaseOutput.t:147 */
                if (base.follow.get(nt).get(t)) /* BaseOutput.t:148 */
                { /* BaseOutput.t:149 */
                    out.print(grammar.restoreSymbol(grammar.retrieveString(t))); /* BaseOutput.t:150 */
                } /* BaseOutput.t:151 */
            } /* BaseOutput.t:152 */

            writer.endAutoWrap(); /* BaseOutput.t:154 */
        } /* BaseOutput.t:155 */
        out.write("\n"); /* BaseOutput.t:156~157 */
    } /* BaseOutput.t:158 */

    void printSymbolMap(String header, BitSet[] map) /* BaseOutput.t:160 */
    { /* BaseOutput.t:161 */
        // /* BaseOutput.t:162 */
        // First, flush any data left in the report buffer. /* BaseOutput.t:163 */
        // /* BaseOutput.t:164 */
        option.flushReport(); /* BaseOutput.t:165 */

        control.printHeading(); /* BaseOutput.t:167 */
        out.write("\n"); /* BaseOutput.t:168~169 */
        out.print(header); /* BaseOutput.t:169 */
        out.write(":\n"); /* BaseOutput.t:169~170 */
        for (int symbol = grammar.firstTerminal(); symbol <= grammar.lastTerminal(); symbol++) /* BaseOutput.t:171 */
        { /* BaseOutput.t:172 */
            out.write("\n\n"); /* BaseOutput.t:173~175 */
            out.print(grammar.restoreSymbol(grammar.retrieveString(symbol))); /* BaseOutput.t:175 */

            writer.beginAutoWrap(); /* BaseOutput.t:177 */
            writer.setIndentSize(4); /* BaseOutput.t:178 */

            out.write(" ==>>"); /* BaseOutput.t:180 */
            for (int t = grammar.firstTerminal(); t <= grammar.lastTerminal(); t++) /* BaseOutput.t:181 */
            { /* BaseOutput.t:182 */
                if (map[symbol].get(t)) /* BaseOutput.t:183 */
                { /* BaseOutput.t:184 */
                    out.print(grammar.restoreSymbol(grammar.retrieveString(t))); /* BaseOutput.t:185 */
                } /* BaseOutput.t:186 */
            } /* BaseOutput.t:187 */

            writer.endAutoWrap(); /* BaseOutput.t:189 */
        } /* BaseOutput.t:190 */
        out.write("\n"); /* BaseOutput.t:191~192 */
    } /* BaseOutput.t:193 */

    void printRank(List<IntArrayList> sccInRanks, IntArrayList orderedRules, int[] rank) /* BaseOutput.t:195 */
    { /* BaseOutput.t:196 */
        if (!sccInRanks.isEmpty()) /* BaseOutput.t:197 */
        { /* BaseOutput.t:198 */
            out.write("\n\nStrongly-connected components in ranks:\n\n"); /* BaseOutput.t:199~203 */
            for (int i = 0, n = sccInRanks.size(); i < n; i++) /* BaseOutput.t:204 */
            { /* BaseOutput.t:205 */
                IntArrayList sccInRanksList = sccInRanks.get(i); /* BaseOutput.t:206 */
                for (int k = 0, m = sccInRanksList.size(); k < m; k++) /* BaseOutput.t:207 */
                { /* BaseOutput.t:208 */
                    out.write("    "); /* BaseOutput.t:209 */
                    out.print(grammar.retrieveString(sccInRanksList.get(k))); /* BaseOutput.t:209 */
                    out.write("\n"); /* BaseOutput.t:209~210 */
                } /* BaseOutput.t:211 */
                out.write("\n"); /* BaseOutput.t:212~213 */
            } /* BaseOutput.t:214 */
        } /* BaseOutput.t:215 */

        out.write("\n\nRANK RULE\n==== ====\n"); /* BaseOutput.t:217~221 */
        for (int i = 0, n = orderedRules.size(); i < n; i++) /* BaseOutput.t:222 */
        { /* BaseOutput.t:223 */
            int ruleNo = orderedRules.get(i); /* BaseOutput.t:224 */
            out.print(rank[ruleNo]); /* BaseOutput.t:225 */
            writer.padToColumn(5); /* BaseOutput.t:226 */
            out.print(ruleNo); /* BaseOutput.t:227 */
            writer.padToColumn(10); /* BaseOutput.t:228 */

            writer.beginAutoWrap(); /* BaseOutput.t:230 */
            out.print(grammar.retrieveString(grammar.rules.get(ruleNo).lhs)); /* BaseOutput.t:231 */
            out.write("::="); /* BaseOutput.t:231 */
            for (int k = grammar.firstRhsIndex(ruleNo); k < grammar.endRhsIndex(ruleNo); k++) /* BaseOutput.t:232 */
            { /* BaseOutput.t:233 */
                int symbol = grammar.rhsSym.get(k); /* BaseOutput.t:234 */
                out.print(grammar.retrieveString(symbol)); /* BaseOutput.t:235 */
            } /* BaseOutput.t:236 */
            writer.endAutoWrap(); /* BaseOutput.t:237 */
            out.write("\n"); /* BaseOutput.t:238~239 */
        } /* BaseOutput.t:240 */
    } /* BaseOutput.t:241 */

}
